﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JGSY.LHBS
{
    public partial class FrmPathConfig : Form
    {
        public FrmPathConfig()
        {
            InitializeComponent();
        }

        private void btSelect_Click(object sender, EventArgs e)
        {
            //using (OpenFileDialog openFileDialog = new OpenFileDialog())
            //{
            //    openFileDialog.Title = "选择文件";
            //    openFileDialog.Filter = "所有文件|*.*";
            //    if (openFileDialog.ShowDialog() == DialogResult.OK)
            //    {
            //        tbPath.Text = openFileDialog.FileName;
            //    }
            //}
            using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
            {
                folderDialog.Description = "选择文件夹"; // 设置对话框提示文本
                folderDialog.RootFolder = Environment.SpecialFolder.Desktop; // 从桌面开始浏览
                folderDialog.ShowNewFolderButton = true; // 允许用户创建新文件夹

                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    tbPath.Text = folderDialog.SelectedPath; // 获取选择的文件夹路径
                }
            }
        }

        private void PathConfig_Load(object sender, EventArgs e)
        {
            if (CommonHelper.storePath != "")
                tbPath.Text = CommonHelper.storePath;
            if (CommonHelper.itemPath != "")
                tbFileDir.Text = CommonHelper.itemPath.Split('/')[1];
            if (CommonHelper.incomePath != "")
                tbSY.Text = CommonHelper.incomePath.Split('/')[1];
            if (CommonHelper.expendPath != "")
                tbZC.Text = CommonHelper.expendPath.Split('/')[1];
            if (CommonHelper.dayPath != "")
                tbDay.Text = CommonHelper.dayPath.Split('/')[1];
            if (CommonHelper.weekPath != "")
                tbZ.Text = CommonHelper.weekPath.Split('/')[1];
            if (CommonHelper.monthPath != "")
                tbM.Text = CommonHelper.monthPath.Split('/')[1];

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string txtPath = tbPath.Text.Trim();
            string txtName = tbFileDir.Text.Trim();
            string txtSR = tbSY.Text.Trim();
            string txtZC = tbZC.Text.Trim();
            string txtDay = tbDay.Text.Trim();
            string txtZ = tbZ.Text.Trim();
            string txtM = tbM.Text.Trim();

            #region 校验数据

            if (string.IsNullOrEmpty(txtPath))
            {
                MessageHelper.Error("路径配置", "请选择存储位置");
                tbPath.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtName))
            {
                MessageHelper.Error("路径配置", "名目数据文件不能为空");
                tbFileDir.Focus();
                return;
            }
            if (Path.GetExtension(txtName) != ".txt")
            {
                MessageHelper.Error("路径配置", "名目数据文件类型不正确（.txt）");
                tbFileDir.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtSR))
            {
                MessageHelper.Error("路径配置", "收入数据文件不能为空");
                tbSY.Focus();
                return;
            }
            if (Path.GetExtension(txtSR) != ".txt")
            {
                MessageHelper.Error("路径配置", "收入数据文件类型不正确（.txt）");
                tbSY.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtZC))
            {
                MessageHelper.Error("路径配置", "支出数据文件不能为空");
                tbZC.Focus();
                return;
            }
            if (Path.GetExtension(txtZC) != ".txt")
            {
                MessageHelper.Error("路径配置", "支出数据文件类型不正确（.txt）");
                tbZC.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDay))
            {
                MessageHelper.Error("路径配置", "日统计数据文件不能为空");
                tbDay.Focus();
                return;
            }
            if (Path.GetExtension(txtDay) != ".txt")
            {
                MessageHelper.Error("路径配置", "日统计数据文件类型不正确（.txt）");
                tbDay.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtZ))
            {
                MessageHelper.Error("路径配置", "周统计数据文件不能为空");
                tbFileDir.Focus();
                return;
            }
            if (Path.GetExtension(txtZ) != ".txt")
            {
                MessageHelper.Error("路径配置", "周统计数据文件类型不正确（.txt）");
                tbZ.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtM))
            {
                MessageHelper.Error("路径配置", "月统计数据文件不能为空");
                tbM.Focus();
                return;
            }
            if (Path.GetExtension(txtM) != ".txt")
            {
                MessageHelper.Error("路径配置", "月统计数据文件类型不正确（.txt）");
                tbM.Focus();
                return;
            }
            #endregion
           
            //文件路径
            txtName = txtPath + "/" + txtName;    
            txtSR = txtPath + "/" + txtSR;          
            txtZC = txtPath + "/" + txtZC;
            txtDay = txtPath + "/" + txtDay;
            txtZ = txtPath + "/" + txtZ;
            txtM = txtPath + "/" + txtM;

            //创建文件夹路径
            if (!Directory.Exists(txtPath))
            {
                Directory.CreateDirectory(txtPath);
            }

            string setFilePath = txtPath + "/DataFileSet.txt"; // 配置文件路径
            AppHelper.SetValue("dataFilePath", setFilePath);//appsetting
            TextHelper.WriteMsg("数据文件路径", setFilePath, false);
            List<string> list = new List<string>();
            list.Add("名目数据文件: " + txtName);
            list.Add("收入数据文件: " + txtSR);
            list.Add("支出数据文件: " + txtZC);
            list.Add("日统计数据文件: " + txtDay);
            list.Add("周统计数据文件: " + txtZ);
            list.Add("月统计数据文件: " + txtM);
            TextHelper.WriteMsgList(list, setFilePath, true);
            MessageHelper.Info("路径配置", "路径配置保存成功！");

            //初始化各路径信息变量
            CommonHelper.storePath = txtPath;
            CommonHelper.setFilePath = setFilePath;
            CommonHelper.itemPath = txtName;
            CommonHelper.incomePath = txtSR;
            CommonHelper.expendPath = txtZC;
            CommonHelper.dayPath = txtDay;
            CommonHelper.weekPath = txtZ;
            CommonHelper.monthPath = txtM;






        }
    }
}
